package com.adv.crm.controller;

import com.adv.crm.beans.UserBean;
import com.adv.crm.beans.WarpResponse;
import com.adv.crm.common.SecurityHelper;
import com.adv.crm.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 16978 on 2016/8/6.
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String loginView() {
        return "login";
    }

    @RequestMapping("json/login")
    @ResponseBody
    public WarpResponse<String> login(HttpServletRequest request, String userName, String userPwd) {
        System.out.println(userName+"---");
        WarpResponse<String> response = new WarpResponse<String>();
        UserBean user = userService.getUserByName(userName);
        if (user == null) {
            response.setSuccess(false);
            response.setMsg("用户不存在");
        } else if (!user.getUserPwd().equalsIgnoreCase(SecurityHelper.MD5Encrypt(userPwd))) {
            response.setSuccess(false);
            response.setMsg("用户密码不正确");
        } else {
            request.getSession().setAttribute("user", user);
        }
        return response;
    }
}
